package com.sun.lib;
/*
 *
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by ESSC07 on 2021/12/21.
 */
public class SViewHelper<T extends View> {
    protected T mView;
    private Context context;


    private Paint shadowPaint;
    private float cornerRadius;
    private float shadowRadius;
    private int shadowColor;
    private float mDx;
    private float mDy;
    private boolean hideBottom;
    private boolean hideLeft;
    private boolean hideRight;
    private boolean hideTop;
    private RectF rectf = new RectF();
    private int leftPadding;
    private int rightPadding;
    private int topPadding;
    private int bottomPadding;
    private Paint paint;

    public SViewHelper(Context context, T view, AttributeSet attrs) {
        this.context = context;
        this.mView = view;
        initAttrs(attrs);
        initView(context);
    }

    private void initView(Context context) {
        shadowPaint = new Paint();
        shadowPaint.setStyle(Paint.Style.FILL);
        shadowPaint.setAntiAlias(true);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);

        /*if(getBackground() instanceof ColorDrawable) {
            paint.setColor(((ColorDrawable) getBackground()).getColor());
        }*/
        paint.setColor(Color.GREEN);

        setPadding();
    }
    // 根据阴影设置padding
    private void setPadding() {
        if (isShowShadow()) {
            int xPadding = (int) (shadowRadius + Math.abs(mDx));
            int yPadding = (int) (shadowRadius + Math.abs(mDy));
            leftPadding = mView.getPaddingLeft();
            rightPadding = mView.getPaddingRight();
            topPadding = mView.getPaddingTop();
            bottomPadding = mView.getPaddingBottom();
            if (!hideLeft) leftPadding += xPadding;
            if (!hideRight) rightPadding += xPadding;
            if (!hideTop) topPadding += yPadding;
            if (!hideBottom) bottomPadding += yPadding;
            mView.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
        }
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SFrameLayout);
        cornerRadius = array.getDimension(R.styleable.SFrameLayout_corner_radius, 0f);
        shadowRadius = array.getDimension(R.styleable.SFrameLayout_shadow_radius, dp2px(5));
        shadowColor = array.getColor(R.styleable.SFrameLayout_shadow_color, Color.parseColor("#2C000000"));
        mDx = array.getDimension(R.styleable.SFrameLayout_shadow_offset_X, 0);
        mDy = array.getDimension(R.styleable.SFrameLayout_shadow_offset_Y, 0f);
        hideBottom = array.getBoolean(R.styleable.SFrameLayout_shadow_hidden_bottom, false);
        hideLeft = array.getBoolean(R.styleable.SFrameLayout_shadow_hidden_left, false);
        hideRight = array.getBoolean(R.styleable.SFrameLayout_shadow_hidden_right, false);
        hideTop = array.getBoolean(R.styleable.SFrameLayout_shadow_hidden_top, false);
        array.recycle();
    }


    private void setBackgroundCompat(int w, int h) {
        if (isShowShadow()) {
            Bitmap bitmap = createShadowBitmap(w, h, cornerRadius, shadowRadius, mDx, mDy, shadowColor, Color.TRANSPARENT);
            BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
                mView.setBackgroundDrawable(bitmapDrawable);
            } else {
                mView.setBackground(bitmapDrawable);
            }
        }
    }
    // 创建透明阴影图片
    private Bitmap createShadowBitmap(int w, int h, float cornerRadius, float shadowRadius, float dx, float dy, int shadowColor, int color) {
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bitmap);
        float left = shadowRadius;
        float right = w - shadowRadius;
        float top = shadowRadius;
        float bottom = h - shadowRadius;
        RectF rectF = new RectF(left, top, right, bottom);
        rectF.top += dy;
        rectF.bottom -= dy;
        rectF.left += dx;
        rectF.right -= dx;
        shadowPaint.setColor(color);
        if (!mView.isInEditMode())
            shadowPaint.setShadowLayer(shadowRadius / 2, dx, dy, shadowColor);
        if (cornerRadius > 0) {
            canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, shadowPaint);
        } else {
            rectf.left = leftPadding;
            rectf.top = topPadding;
            rectf.right = mView.getWidth() - rightPadding;
            rectf.bottom = mView.getHeight() - bottomPadding;
            float[] outR = new float[]{cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius};
            Path path = new Path();
            path.addRoundRect(rectF, outR, Path.Direction.CW);
            canvas.drawPath(path, shadowPaint);
        }
        return bitmap;
    }


    private boolean isShowShadow() {
        return shadowRadius > 0;
    }


    private Context getContext() {
        return context;
    }

    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (w > 0 && h > 0)
            setBackgroundCompat(w, h);
    }

    // super.dispatchDraw 之前
    public void dispatchDrawBefore(Canvas canvas) {
        int outHeight = (int) (rectf.bottom - rectf.top);
        if (getChildAt(0) != null) {
            Path path = new Path();
            if (cornerRadius > outHeight / 2) {
                path.addRoundRect(rectf, outHeight / 2, outHeight / 2, Path.Direction.CW);
            } else {
                path.addRoundRect(rectf, cornerRadius, cornerRadius, Path.Direction.CW);
            }
            canvas.clipPath(path);
        } else {

        }
    }

    // super.dispatchDraw 之后
    public void dispatchDrawAfter(Canvas canvas) {
    }

    public void onDraw(Canvas canvas) {
        rectf.left = leftPadding;
        rectf.right = mView.getWidth() - rightPadding;
        rectf.top = topPadding;
        rectf.bottom = mView.getHeight() - bottomPadding;
        int outHeight = (int) (rectf.bottom - rectf.top);
        if (getChildAt(0) != null) {
            if (cornerRadius > outHeight / 2) {
                canvas.drawRoundRect(rectf, outHeight / 2, outHeight / 2, paint);

                /*RectF rectFStroke = new RectF(rectf.left + stroke_with / 2, rectf.top + stroke_with / 2, rectf.right - stroke_with / 2, rectf.bottom - stroke_with / 2);
                canvas.drawRoundRect(rectFStroke, trueHeight / 2-stroke_with / 2, trueHeight / 2-stroke_with / 2, paint_stroke);*/
            } else {
                canvas.drawRoundRect(rectf, cornerRadius, cornerRadius, paint);
            }
        }
    }

    private View getChildAt(int position) {
        if (mView instanceof ViewGroup) {
            return ((ViewGroup) mView).getChildAt(position);
        }
        return null;
    }

    protected int dp2px(float dp) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
